# -*- encoding: utf-8 -*-

# Copyright 2010 István Szentandrási
# <etude-music-player@googlegroups.com>

# This file is part of Etude.
#
# Etude is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as 
# published by the Free Software Foundation, either version 3 
# of the License, or (at your option) any later version.
#
# Etude is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Etude. If not, see <http://www.gnu.org/licenses/>.

import gobject
import xmmsclient
import xmmsclient.collections

from common import *


class Collection(gobject.GObject):

    """
    merely a wrapper class, to create collections the xmmsclient.collections
    classes are used
    for ex:
    from xmmsclient import collections as c

    artist = c.Match(field = "artist", value = "The Rasmus")
    album = c.Has(artist, "album") #example to combine collections
    xmms.coll_query_infos(album, [ "title", "artist"] , cb = foo)
    #this must be a list, tuple is not enough
     ----
    to create collections use these subclasses (same as xmmsclient.collections
    Complement -> ~
    Intersection(*list) -> &
    Union(*list) -> |
    Reference(ref, ns = "Collections")
    Universe()
    Equals(parent = None, **kv)
    Match(parent = None, **kv)
    Smaller(parent = None, **kv)
    Greater(parent = None, **kv)
    IDList()
    Queue()
    PShuffle(parent)
    Has(parent, field)
    http://wiki.xmms2.xmms.se/wiki/Collections_Concept
    
    function parse returns a collection from a given pattern
    """
    Complement = xmmsclient.collections.Complement
    Equals = xmmsclient.collections.Equals
    Greater = xmmsclient.collections.Greater
    Has = xmmsclient.collections.Has
    IDList = xmmsclient.collections.IDList
    Intersection = xmmsclient.collections.Intersection
    Match = xmmsclient.collections.Match
    PShuffle = xmmsclient.collections.PShuffle
    Queue = xmmsclient.collections.Queue
    Reference = xmmsclient.collections.Reference
    Smaller = xmmsclient.collections.Smaller
    Union = xmmsclient.collections.Union
    Universe = xmmsclient.collections.Universe

    def __init__(self, client, coll = None):
        self.__gobject_init__()
        self.client = client
        self.coll = coll
        self.idlist = None
        self.ns = None
        self.name = None

    def coll_parse(self, pattern):
        return xmmsclient.collections.coll_parse(pattern)

    def set_coll(self, coll):
        self.coll = coll

    def get_coll(self):
        return self.coll

    @check_fast(False)
    @check_wait(False)
    def get(self, name, ns, cb=None, cb_args = {}):
        """
        retrieve a collection...
        ns = { Playlists | Collections }
        name - the under which the collection was saved
        """
        if cb == None:
            self.ns = ns
            self.name = name
        callb = lambda result: self._get(result, cb, cb_args)
        return self.client.xmms.coll_get(name, ns, callb)
    
    @check_result_error(True)
    def _get(self, result, cb, cb_args):
        if cb == None:
            self.coll = result.value()
        elif hasattr(cb, '__call__'):
            cb(result.value(), **cb_args)
        return False

    @check_fast(False)
    @check_wait(False)
    @check_cb_None()
    def idlist_from_playlist_file(self, path, cb = None):
        """
        funny function :?
        """
        return self.client.xmms.coll_idlist_from_playlist_file(path, cb)
    
    @check_fast(False)
    @check_wait(False)
    def query_ids(self, coll=None, start = 0, leng = 0, order = None, cb = None, cb_args = {}):
        """
        get ids of a collection
        """
        if cb == None:
            return None
        if coll == None:
            coll = self.coll
            cb_args['fillMeIn'] = True
        return self.client.xmms.coll_query_ids(coll, start, leng, order,
                lambda result: self._query_ids(result, cb, cb_args))
    
    @check_result_error(True)
    def _query_ids(self, result, cb, cb_args = {}):
        if 'fillMeIn' in cb_args:
            self.idlist = result.value()
            del cb_args['fillMeIn']
        if hasattr(cb, '__call__'):
            cb(result.value(), **cb_args)

        return False

    @check_fast(False)
    @check_wait(False)
    def query_infos(self, fields, coll = None, start = 0, leng = 0, order = None, groupby=None, cb = None, cb_args = {}):
        """
        query informations defined in field from the collection coll
        cb_args dictionary of argument for the collback

        """
        if cb == None:
            return None
        if coll == None:
            coll = self.coll
        return self.client.xmms.coll_query_infos(coll, fields, start, leng, order, groupby,
                lambda result: self._query_infos(result, cb, cb_args))

    @check_result_error(True)
    def _query_infos(self, result, cb, cb_args = {}):
        if hasattr(cb, '__call__'):
            cb(result.value(), **cb_args)
        return False

    @check_fast(False)
    @check_wait(False)
    def list(self, namespace = "*", cb = None, cb_args = {}):
        """
        list the collections from namespace
        """
        if cb == None:
            return None
        return self.client.xmms.coll_list(namespace,
                lambda result: self._list(result, cb, cb_args))

    @check_result_error(True)
    def _list(self, result, cb, cb_args = {}):
        if hasattr(cb, '__call__'):
            cb(result.value(), **cb_args)
        return False

    @check_cb_None()
    @check_wait(False)
    @check_fast(False)
    def save(self, coll, name, ns, cb = None):
        """
        save a collection to the server
        """
        return self.client.xmms.coll_save(coll, name, ns, cb)

    @check_cb_None()
    @check_wait(False)
    @check_fast(False)
    def save_this(self, name, ns, cb = None):
        if self.coll:
            return self.client.xmms.coll_save(self.coll, name, ns, cb)
        return None


    @check_cb_None()
    @check_wait(False)
    @check_fast(True)
    def rename(self, oldname, newname, ns, cb = None):
        """
        rename a collection
        ns = namespace
        """
        return self.client.xmms.coll_rename(oldname, newname, ns, cb)
    
    def check_medialist(self, coll):
        """check if the given collection is a medialist"""
        if (isinstance(coll, self.IDList) 
                or isinstance(coll, self.PShuffle) 
                or isinstance(coll, self.Queue)):
            return True
        else:
            return False
    
    @check_cb_None()
    @check_wait(False)
    @check_fast(True)
    def remove(self, name, ns, cb = None):
        """
        remove a collection from the server
        """
        return self.client.xmms.coll_remove(name, ns, cb)

